# Copyright 2018 Tecnativa - Carlos Dauden
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import api, fields, models
from odoo.addons import decimal_precision as dp
from odoo.tools.float_utils import float_compare, float_round
import logging


_logger = logging.getLogger(__name__)

class MrpProduction(models.Model):
    _inherit = 'mrp.production'

    # @api.model
    # def create(self, vals):
    #     rec = super(MrpProduction, self).create(vals)
    #     _logger.info('MrpProduction create')
    #     return rec

    def _generate_raw_move(self, bom_line, line_data):
        stock_move = super(MrpProduction, self)._generate_raw_move(bom_line,line_data)
        _logger.info('_generate_raw_move')
        _logger.info(line_data['original_qty'])
        _logger.info(bom_line['product_qty'])
        stock_move.write({'secondary_uom_qty_bom':line_data['original_qty'],'secondary_uom_tx_bom':bom_line['product_qty']})
        return stock_move

class StockMove(models.Model):
    _inherit = 'stock.move'
    secondary_uom_qty_bom = fields.Float(
        string=u'数量(桶)',
        digits=dp.get_precision('Product Unit of Measure'),
    )
    secondary_uom_tx_bom = fields.Float(
        string=u'规格(kg/桶)',
        ondelete='restrict',
    )
    secondary_uom_qty = fields.Float(
        string=u'数量(桶)',
        digits=dp.get_precision('Product Unit of Measure'),
        compute='_compute_qty'
    )
    secondary_uom_tx = fields.Float(
        string=u'规格(kg/桶)',
        ondelete='restrict',
        compute='_compute_qty'
    )

    @api.depends('sale_line_id','purchase_line_id')
    def _compute_qty(self):
        _logger.info('_compute_qty')
        for move in self:
            if move.sale_line_id :
                move.update({
                    'secondary_uom_tx': move.sale_line_id.secondary_uom_tx,
                    'secondary_uom_qty': move.sale_line_id.secondary_uom_qty
                })
            if move.purchase_line_id:
                move.update({
                    'secondary_uom_tx': move.purchase_line_id.secondary_uom_tx,
                    'secondary_uom_qty': move.purchase_line_id.secondary_uom_qty
                })
